/*
 * @(#)PersonNameTransformer.java
 * Date 2013-08-16
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.pix.transformer;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.COCTMT030007UVPerson;
import org.hl7.v3.COCTMT150003UV03Person;
import org.hl7.v3.COCTMT150007UVPerson;
import org.hl7.v3.EN;
import org.hl7.v3.ENXP;

import us.hornerscorners.lamppost.config.ModelConfig;

import us.hornerscorners.lamppost.exception.ServiceActionException;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;

import us.hornerscorners.lamppost.model.common.CodedElement;

import us.hornerscorners.lamppost.model.patient.PersonName;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class PersonNameTransformer extends AbstractTransformer<EN, PersonName> {

    /** Field description */
    @Inject
    private IHEConfig iheConfig;

    /**
     * Constructs ...
     *
     */
    public PersonNameTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param prpaName
     *
     * @return
     */
    @Override
    public PersonName transform(EN prpaName) {

        PersonName result = null;

        if (prpaName != null) {

            result = new PersonName();

            String usestr = HL7Utils.parseFirstString(prpaName.getUse());

            if (StringUtils.isNotBlank(usestr)) {

                ModelConfig modelConfig = this.iheConfig.getModelConfig();

                CodedElement elem = new CodedElement();

                elem.setCode(usestr);
                elem.setCodeSystem(modelConfig.getNameUseCodeSystem());
                result.setPurpose(elem);
            }

            boolean givenNameDone = false;

            for (Serializable serializable : prpaName.getContent()) {

                if (serializable instanceof JAXBElement) {

                    JAXBElement jaxbElement = (JAXBElement) serializable;
                    String value = HL7Utils.parseFirstSerializable(
                                       ((ENXP) jaxbElement.getValue()).getContent());
                    String tagName = jaxbElement.getName().getLocalPart();

                    switch (tagName) {

                        case "family" :
                            result.setLastName(value);

                            break;

                        case "given" :
                            if (givenNameDone == false) {

                                result.setFirstName(value);
                                givenNameDone = true;

                            } else {

                                result.getMiddleNames().add(value);
                            }

                            break;

                        case "prefix" :
                            result.setPrefix(value);

                            break;

                        case "suffix" :
                            result.setSuffix(value);

                            break;
                    }
                }
            }
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param jaxbPerson
     *
     * @return
     * @throws us.hornerscorners.lamppost.exception.ServiceActionException
     */
    public List<PersonName> transformCOCTMT030007UVPerson(
            JAXBElement<COCTMT030007UVPerson> jaxbPerson)
            throws ServiceActionException {

        List<PersonName> result = new ArrayList<>();

        if ((jaxbPerson != null) && (jaxbPerson.getValue() != null)) {

            result.addAll(transform(jaxbPerson.getValue().getName()));
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param jaxbPerson
     *
     * @return
     * @throws us.hornerscorners.lamppost.exception.ServiceActionException
     */
    public List<PersonName> transformCOCTMT150003UV03Person(
            JAXBElement<COCTMT150003UV03Person> jaxbPerson)
            throws ServiceActionException {

        List<PersonName> result = new ArrayList<>();

        if ((jaxbPerson != null) && (jaxbPerson.getValue() != null)) {

            result.addAll(transform(jaxbPerson.getValue().getName()));
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @param jaxbPerson
     *
     * @return
     * @throws us.hornerscorners.lamppost.exception.ServiceActionException
     */
    public List<PersonName> transformCOCTMT150007UVPerson(
            JAXBElement<COCTMT150007UVPerson> jaxbPerson)
            throws ServiceActionException {

        List<PersonName> result = new ArrayList<>();

        if ((jaxbPerson != null) && (jaxbPerson.getValue() != null)) {

            result.addAll(transform(jaxbPerson.getValue().getName()));
        }

        return result;
    }
}
